//Page Object
import {
    request
} from "../../request/index.js"
Page({
    data: {
        //左边菜单数据
        leftmenuList: [],
        //右侧商品数据
        rightcontentList: [],
        //被点击左侧菜单
        currentIndex: 0,
        //右侧滑动条位置
        rightscroll: 0
    },
    Cates: [],
    //options(Object)
    onLoad: function (options) {
        // 判断数据是否具有本地缓存
        const Cates = wx.getStorageSync("cates");
        if (!Cates) {
            this.getCates()
        } else {
            if (Date.now() - Cates.time > 1000 * 10) {
                this.getCates()
            } else {
                this.Cates = Cates.data
                let leftmenuList = this.Cates.map(v => v.cat_name);
                //
                let rightcontentList = this.Cates[0].children;
                this.setData({
                    leftmenuList,
                    rightcontentList,

                })
            }
        }

    },
    onReady: function () {

    },
    onShow: function () {

    },
    onHide: function () {

    },
    onUnload: function () {

    },
    onPullDownRefresh: function () {

    },
    onReachBottom: function () {

    },
    onShareAppMessage: function () {

    },
    onPageScroll: function () {

    },
    //item(index,pagePath,text)
    onTabItemTap: function (item) {

    },
    getCates() {

        request({
            url: '/categories'
        }).then(res => {
            this.Cates = res.data.message;
            wx.setStorageSync("cates", {
                time: Date.now(),
                data: this.Cates
            })
            //
            let leftmenuList = this.Cates.map(v => v.cat_name);
            //
            let rightcontentList = this.Cates[0].children;
            this.setData({
                leftmenuList,
                rightcontentList
            })
        })
    },



    handleclick(e) {
        const {
            index
        } = e.currentTarget.dataset;
        let rightcontentList = this.Cates[index].children;
        this.setData({
            currentIndex: index,
            rightcontentList,
            rightscroll: 0

        })

    }
});